package com.rockcent.third.utils;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.util.Map;

/**
 * Freemarker工具类
 */
public class FreemarkerUtil {

    public static String parseString(String content, Map<String, Object> params) throws Exception {
        Configuration stringConfig = new Configuration(Configuration.VERSION_2_3_23);
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        stringLoader.putTemplate("_template_", content);
        stringConfig.setTemplateLoader(stringLoader);
        Template tpl = stringConfig.getTemplate("_template_", "utf-8");
        return FreeMarkerTemplateUtils.processTemplateIntoString(tpl, params);
    }

}